"""
@author: jiangpj
@email: pj.jiang@hzgosun.com
@file: userRoleManageTest.py
@time: 2019/5/24 11:32

后台角色的增删改查自动化测试用例编写

"""
from common.functions import save_screenshot
import unittest
from time import sleep
from selenium.webdriver.common.keys import Keys

from common.decorators import log4gs, mkdir_classname
from common.settings import TAG
from common.vue.select import OperateSelect
from pages.userRoleManagePage import UserRole
name_init = "自动化测试"
name_copy = "后台角色"

@log4gs
@mkdir_classname(TAG)
class UserRoleTest(unittest.TestCase):
    """后台角色管理功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始后台角色管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = UserRole('/UserManage/UserRoleManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束后台角色管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        sleep(2)
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))

    # @unittest.skip
    def test_1_page(self):
        """后台角色翻页"""
        sleep(1)
        self.log.info("共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.info("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.info("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        if int(self.page.turnPage.total_page) >= 3:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')
            sleep(2)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(3)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '3')
        else:
            self.log.error("当前模块最大未到3页，无法进行翻页测试", exc_info=True)
            save_screenshot(self, '翻页功能测试失败')

    def test_2_add_UserRole(self):
        """新增功能验证"""
        name = name_init
        self.page.table.add_btn.click()
        sleep(1)
        self.page.addUserRole.idname.send_keys(name)
        self.page.addUserRole.page_role.select("设备管理")
        sleep(2)
        self.page.addUserRole.confirm.click()
        sleep(1)
        self.page.searchTools.number.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        result = self.page.table.get_row(name)
        self.page.searchTools.number.clear()
        sleep(1)
        try:
            self.assertTrue(result, "无法查找到目标：{}".format(name))
            self.log.info("用例{}新增功能执行成功".format(self.id()))
        except Exception:
            self.log.error("用例{}新增功能执行失败".format(self.id()), exc_info=True)

    def test_3_searck_UserRole(self):
        """后台角色名称检索验证"""
        name = name_init
        self.page.searchTools.number.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(name)
        sleep(1)
        self.page.searchTools.number.clear()
        try:
            self.assertTrue(result, "查询到了{}".format(name))
            self.log.info("用例后台角色名称检索执行成功")
        except Exception:
            self.log.error("用例后台角色名称检索执行失败", exc_info=True)

    def test_4_modify_UserRole(self):
        """修改前台用户角色"""
        name = name_init
        name2 = name_copy
        self.page.searchTools.number.send_keys(name)
        self.page.searchTools.search_btn.click()
        sleep(1)

        search_result = self.page.table.get_row(name)
        OperateSelect(search_result, "修改")
        sleep(1)
        self.page.modifyUserRole.idname.clear()
        self.page.modifyUserRole.idname.send_keys(name2)
        self.page.modifyUserRole.page_role.select("基础数据")
        self.page.modifyUserRole.confirm.click()
        sleep(1)
        self.page.searchTools.number.clear()
        sleep(1)
        self.page.searchTools.number.send_keys(name2)
        self.page.searchTools.search_btn.click()

        modify_search_result = self.page.table.get_row(name2)
        self.page.searchTools.number.clear()
        try:
            self.assertTrue(modify_search_result)
            self.log.info("后台角色管理修改成功")
        except Exception:
            self.log.error("后台角色管理修改失败,", exc_info=True)

    def test_5_block_up_start_UserRole(self):
        """停/启用后台角色"""
        name = name_copy
        self.page.searchTools.number.send_keys(name)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.number.clear()
        search_result = self.page.table.get_row(name)
        OperateSelect(search_result, "停用")
        sleep(2)
        block_up_result = self.page.table.get_row("停用")
        try:
            self.assertTrue(block_up_result, msg="后台角色停用成功")
            self.log.info("停用后台角色停用执行成功")
        except Exception:
            self.log.error("停用后台角色停用执行失败", exc_info=True)
        sleep(2)
        OperateSelect(search_result, "启用")
        sleep(2)
        start_up_result = self.page.table.get_row("启用")
        try:
            self.assertTrue(start_up_result, msg="后台角色启用成功")
            self.log.info("启用后台角色启用执行成功")
        except Exception:
            self.log.error("启用后台角色启用执行失败", exc_info=True)


if __name__ == '__main__':
    unittest.main()
